function Player(){
	
	this.moves = [];
	
	this.rotationVector = 0;
	this.rotationAngle = 0;
	this.rotationCounter = 0;
	this.duration = 1000;
	
	this.numberStepsToPlay = 0;
	
	this.last = {playDirection:true};
	this.current = {
			step: 0, 
			playDirection: false  //true:forward 
	};
	
	this.loadHistory = function(arr){
		//this.moves = arr;
		this.moves = [];
		for(var i=0; i<arr.length; i++){
			var b = new HistoryRecord;
			for( var property in arr[i] ){
				try{
					
					b[property] = arr[i][property];
				}
				catch( warning ){console.log(warning);} //TODO: remove
			}
			this.moves.push(b);
		}
		
	};
	
	this.startPlay = function(){
		
		if(this.current.playDirection && this.last.playDirection && this.current.step+1 == this.moves.length){
			console.log('can not play forward'); //TODO: remove
			return true;
		}
		
		if((this.current.playDirection == false) && !this.last.playDirection && (this.current.step == 0)){
			console.log('can not play backward'); //TODO: remove
			return true;
		}
		
		controller.enableInputs = false;
		models.saveToBuffer();
		controller.sleep();
		this.playStep();
	};
	
	this.onStepEnd = function(){
		this.numberStepsToPlay--;
		if(this.numberStepsToPlay == 0){
			this.endPlay();
			return true;
		}
		
		if(this.current.playDirection){
			if(this.current.step+1 == this.moves.length){
				this.endPlay();
				return true;
			}
			models.saveToBuffer();
			this.current.step++;
			this.playStep();
		}else{
			if(this.current.step == 0){
				this.endPlay();
				return true;
			}
			models.saveToBuffer();
			this.current.step--;
			this.playStep();
		}
	};
	
	this.endPlay = function(){
		this.numberStepsToPlay = 0;
		controller.enableInputs = true;
		this.last.playDirection = (this.current.playDirection)? true:false;
		//console.log(movesHistory);
		//console.log(this.current.step); 
		
		movesHistory.setCurrentPointer(this.current.step);
	};
	
	this.playStep = function(){
		//console.log(this.current.step + ' - ' + movesHistory.current);
		
		//if play back, revert rotation
		if(!this.current.playDirection){
			this.moves[this.current.step].angle = this.moves[this.current.step].angle * -1;
		}
		
		if(player.moves[player.current.step].angle > 0){
			player.rotationVector = 1;
		}else{
			player.rotationVector = -1;
		}
		/*
		animate({
			playBy: 'player',
			delay: 20,
			duration: player.duration,
			delta:makeEaseOut(circ),
			step: function(delta) {
				rotation_data = player.moves[player.current.step];
				controller.rotationAngle = 90 * delta * player.rotationVector;
				//console.log(controller.rotationAngle);
				models.loadBuffer();
				models.updateByGroup( rotation_data.vector ,rotation_data.level[rotation_data.vector], rotation_data.fname, controller.rotationAngle);
				models.updateVertices();
				engine.loadVertices(vertices.polygons);
				engine.display();
				if(controller.rotationAngle >= 90 || controller.rotationAngle <= -90){
					controller.rotationAngle = 0;
					controller.rotationVector = 0;
					controller.rotationCounter = 0;
				}
			}
		});
		*/
		
		animate({
			playBy: 'player',
			delay: 20,
			duration: player.duration,
			delta:makeEaseOut(circ),
			step: function(delta) {
				rotation_data = player.moves[player.current.step];
				player.rotationAngle = 90 * delta * player.rotationVector;
				//console.log(controller.rotationAngle);
				models.loadBuffer();
				models.updateByGroup( rotation_data.vector ,rotation_data.level[rotation_data.vector], rotation_data.fname, player.rotationAngle);
				models.updateVertices();
				engine.loadVertices(vertices.polygons);
				engine.display();
				if(player.rotationAngle >= 90 || player.rotationAngle <= -90){
					player.rotationAngle = 0;
					player.rotationVector = 0;
					player.rotationCounter = 0;
				}
			}
		});
	};
}

var player = new Player();
